const global = require('global');
const audioManger = require('audioManger');
const com = require('Common');
cc.Class({
    extends: cc.Component,

    properties: {
		bgMusic: {type: cc.AudioClip,default: null},
		clearMusic: {type: cc.AudioClip,default: null},
		overMusic: {type: cc.AudioClip,default: null},
		badMusic: {type: cc.AudioClip,default: null},
		gamescene:cc.Prefab,
		selectPanel:cc.Prefab,
		storyPanel:cc.Prefab,
		endlistPanel:cc.Prefab,
		panelnode:cc.Node,
		_panelpool:[],//放置预制件的节点池
		musicpool:{type: cc.AudioClip,default: []},//放置背景音乐的节点池
    },

    onLoad () {
		// 播放背景音乐
		audioManger.init();
		com.soundYinliang = 0.8;
		com.effectYinliang = 0.8;
        audioManger.playerBgMusic(this.bgMusic);
		//cc.audioEngine.play(this.bgMusic, true, 1);
		//global.bgmId = cc.audioEngine.play(this.bgMusic, true, 1);
		//cc.log('main:global.bgmId='+bgid);
		//this.playBgm();

		//监听关卡选择id
		cc.director.on('qkselect',(e)=>{
			let gamescene = cc.instantiate(this.gamescene);
			gamescene.parent = this.panelnode;
			this._panelpool[0] = gamescene;
			cc.director.emit('gamescene',e.detail);
		},this);
		
 		//监听背景音乐修改
		cc.director.on('changebg',(e)=>{
			cc.log('changebg on==========');
			if(e.detail.name == 'music'){//背景音乐
				cc.log('changebg to music');
				cc.audioEngine.stopAll();
				cc.audioEngine.play(this.bgMusic, true, 1);
			}else if(e.detail.name == 'bad'){
				cc.log('changebg to bad');
				cc.audioEngine.stopAll();
				//cc.audioEngine.pause(global.badBgmId);
				cc.audioEngine.play(this.badMusic, true, 1);
			}else if(e.detail.name == 'clear'){
				cc.log('changebg to clear');
				cc.audioEngine.stopAll();
				//cc.audioEngine.pause(global.clearBgmId);
				cc.audioEngine.play(this.clearMusic, true, 1);
			}else if(e.detail.name == 'gameover'){
				cc.log('changebg to gameover');
				cc.audioEngine.stopAll();
				//cc.audioEngine.pause(global.overBgmId);
				cc.audioEngine.play(this.overMusic, true, 1);
			}
		},this);
    },
	//测试用修改背景音乐
	changebgm(){
		let r = Math.random()*4;
		cc.log('------changebgm:r='+r);
		if(r>3){
			cc.director.emit('changebg',{name:'bad'});
		}else if(r>2){
			cc.director.emit('changebg',{name:'clear'});
		}else{
			cc.director.emit('changebg',{name:'gameover'});
		}
		
	},
	onBtnClick(event,customData){		
        switch(customData){
            case 'select': 
				//console.log('-=-=-=任务完成');
				if(global.gkjindu > 0){
					let selectPanel = cc.instantiate(this.selectPanel);
					selectPanel.parent = this.panelnode;
				}else{//刚开始玩时直接进入起始对话id为0的gamescene
					this.gotostory();
					
				}
				break;
			case 'story':
				//story模式就是固定打开起始对话id为0的gamescene
				this.gotostory();
				break;
            case 'endlist':
				let endlistPanel = cc.instantiate(this.endlistPanel);
				endlistPanel.parent = this.panelnode;
				break;
            default:
				break;

        }
    },
    gotostory(){
		let gamescene = cc.instantiate(this.gamescene);
		gamescene.parent = this.panelnode;
		cc.director.emit('gamescene',0);
	},
	start(){
		//监听关卡选择id
		cc.director.on('returntomain',(e)=>{
			cc.log('returntomain on==========');
			this.panelnode.removeAllChildren;
		},this);
	},
	//测试用打开关卡选择面板
	selectpanel(){
		let selectPanel = cc.instantiate(this.selectPanel);
		selectPanel.parent = this.panelnode;
	}
    // update (dt) {},
});
